001    package net.sf.xdc.util;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import java.util.Properties;
020    import java.util.Enumeration;
021    import java.io.File;
022    
023    import org.apache.log4j.Logger;
024    import org.apache.log4j.PropertyConfigurator;
025    
026    /**
027     * The <code>Logging</code> class is a utility class wrapping the access to the
028     * Apache Log4J logging library.
029     *
030     * @author Jens Voß
031     * @since 0.5
032     * @version 0.5
033     */
034    public class Logging {
035    
036      static {
037        Properties props = new Properties();
038        for (Enumeration names = System.getProperties().propertyNames();
039             names.hasMoreElements();) {
040          String name = (String) names.nextElement();
041          if (name.startsWith("log4j.")) {
042            props.setProperty(name, System.getProperty(name));
043          }
044        }
045        PropertyConfigurator.configure(props);
046        File file = new File("log4j.properties");
047        if (file.exists()) {
048          PropertyConfigurator.configure("log4j.properties");
049        }
050      }
051    
052      /**
053       * This method returns a Log4J <code>Logger</code> with a name equal to the
054       * class from which this method is invoked.
055       *
056       * @return A Log4J <code>Logger</code> with the name of the invoking class
057       */
058      public static Logger getLogger() {
059        String className;
060        try {
061          throw new Exception();
062        }
063        catch (Exception e) {
064          className = e.getStackTrace()[1].getClassName();
065        }
066        return Logger.getLogger(className);
067      }
068    
069      private Logging() {
070      }
071    
072    }